﻿using MySql.Data.MySqlClient;
using PelitaProduksi.Controller;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PelitaProduksi.Model
{
    public class Pegawai : BaseProduksi
    {
        public Pegawai() { }
        public Pegawai(string kode)
        {
            this.Kode = kode;
        }
        public Pegawai(string kode, string nama)
        {
            this.Kode = kode;
            this.Nama = nama;
        }
        public static List<Pegawai> listAllPegawai(bool filter)
        {
            string add = "";
            if (filter) add = "where aktif = 1 ";
            List<Pegawai> list = new List<Pegawai>();
            MySQLController db = new MySQLController();
            db.Open();
            MySqlDataReader reader = db.ExecuteReader(string.Format(
                "select * from karyawan {0} order by nama asc", add));
            while (reader.Read())
            {
                Pegawai p = new Pegawai();
                p.Kode = reader.GetString(0);
                p.Nama = reader.GetString(1);
                p.Kelompok = reader.GetString(2);
                p.Gaji = reader.GetDouble(3);
                p.Gteknisi = reader.GetDouble(4);
                p.Tupdate = reader.GetString(5);
                p.Jupdate = reader.GetString(6);
                p.Usrupdate = reader.GetString(7);
                p.Keterangan = reader.GetString(8);
                p.Aktif = reader.GetBoolean(9);
                p.Klmpgaji = reader.GetString(10);
                p.Gajiobras = reader.GetDouble(11);
                p.Gajisetter = reader.GetDouble(12);
                p.Gajisetter2 = reader.GetDouble(13);
                list.Add(p);
            }
            db.Close();
            return list;
        }
        public static List<string> listAllKelompok()
        {
            List<string> list = new List<string>();
            MySQLController db = new MySQLController();
            db.Open();
            MySqlDataReader reader = db.ExecuteReader("select kelompok, count(*) from karyawan where aktif = 1 group by kelompok having count(kelompok) <= 3 order by kelompok asc");
            while (reader.Read())
            {
                string s = reader.GetString(0);
                if (s.Trim().Length > 0)
                {
                    // Fill into list
                    list.Add(s);
                }
            }
            db.Close();
            return list;
        }
        public static List<Pegawai> listAllPegawaiByKelompok(string kelompok)
        {
            List<Pegawai> list = new List<Pegawai>();
            MySQLController db = new MySQLController();
            db.Open();
            MySqlDataReader reader = db.ExecuteReader(string.Format(
                "select * from karyawan where kelompok = '{0}' and aktif = 1 order by nama", kelompok));
            while (reader.Read())
            {
                Pegawai p = new Pegawai();
                p.Kode = reader.GetString(0);
                p.Nama = reader.GetString(1);
                p.Kelompok = reader.GetString(2);
                p.Gaji = reader.GetDouble(3);
                p.Gteknisi = reader.GetDouble(4);
                p.Tupdate = reader.GetString(5);
                p.Jupdate = reader.GetString(6);
                p.Usrupdate = reader.GetString(7);
                p.Keterangan = reader.GetString(8);
                p.Aktif = reader.GetBoolean(9);
                p.Klmpgaji = reader.GetString(10);
                p.Gajiobras = reader.GetDouble(11);
                p.Gajisetter = reader.GetDouble(12);
                p.Gajisetter2 = reader.GetDouble(13);
                list.Add(p);
            }
            db.Close();
            return list;
        }

        public override string ToString()
        {
            return Nama;
        }

        string kode;

        public string Kode
        {
            get { return kode; }
            set { kode = value; }
        }
        string nama;

        public string Nama
        {
            get { return nama; }
            set { nama = value; }
        }
        string kelompok;

        public string Kelompok
        {
            get { return kelompok; }
            set { kelompok = value; }
        }
        double gaji;

        public double Gaji
        {
            get { return gaji; }
            set { gaji = value; }
        }
        double gteknisi;

        public double Gteknisi
        {
            get { return gteknisi; }
            set { gteknisi = value; }
        }
        string tupdate;

        public string Tupdate
        {
            get { return tupdate; }
            set { tupdate = value; }
        }
        string jupdate;

        public string Jupdate
        {
            get { return jupdate; }
            set { jupdate = value; }
        }
        string usrupdate;

        public string Usrupdate
        {
            get { return usrupdate; }
            set { usrupdate = value; }
        }
        string keterangan;

        public string Keterangan
        {
            get { return keterangan; }
            set { keterangan = value; }
        }
        bool aktif;

        public bool Aktif
        {
            get { return aktif; }
            set { aktif = value; }
        }
        string klmpgaji;

        public string Klmpgaji
        {
            get { return klmpgaji; }
            set { klmpgaji = value; }
        }
        double gajiobras;

        public double Gajiobras
        {
            get { return gajiobras; }
            set { gajiobras = value; }
        }
        double gajisetter;

        public double Gajisetter
        {
            get { return gajisetter; }
            set { gajisetter = value; }
        }
        double gajisetter2;

        public double Gajisetter2
        {
            get { return gajisetter2; }
            set { gajisetter2 = value; }
        }

        public override bool Equals(object obj)
        {
            try
            {
                Pegawai p = obj as Pegawai;
                bool ok = true;
                List<bool> test = new List<bool>();
                test.Add(p.Kode.Equals(this.Kode));
                test.Add(p.Nama.Equals(this.Nama));
                test.Add(p.Kelompok.Equals(this.Kelompok));
                test.Add(p.Aktif.Equals(this.Aktif));
                test.Add(p.Keterangan.Equals(this.Keterangan));
                foreach (bool b in test)
                {
                    ok = ok & b;
                }
                return ok;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override void insert()
        {
            MySQLController db = new MySQLController();
            db.Open();
            MySqlTransaction tr = db.BeginTransaction();
            string queryInsert = string.Format(
                "insert into {0} (kode, nama, kelompok, aktif, keterangan) " +
            "VALUES('{1}', '{2}', '{3}', {4}, '{5}')",
                "karyawan",
                this.Kode,
                this.Nama,
                this.Kelompok,
                this.Aktif,
                this.Keterangan
                );
            try
            {
                db.ExecuteNonQuery(queryInsert);
                tr.Commit();
            }
            catch (Exception)
            {
                tr.Rollback();
            }
            db.Close();
        }

        public override void edit(string id)
        {
            MySQLController db = new MySQLController();
            db.Open();
            MySqlTransaction tr = db.BeginTransaction();
            string queryUpdate = string.Format(
                "update {0} SET nama='{1}', kelompok='{2}', keterangan='{3}', aktif={4} where kode='{5}'",
                "karyawan",
                this.Nama,
                this.Kelompok,
                this.Keterangan,
                this.Aktif,
                this.Kode
                );
            try
            {
                db.ExecuteNonQuery(queryUpdate);
                tr.Commit();
            }
            catch (Exception)
            {
                tr.Rollback();
            }
            db.Close();
        }
    }
}
